package com.shensi.adminCollect.controller.station;

import com.alibaba.fastjson.JSONObject;
import com.shensi.common.core.controller.BaseController;
import com.shensi.common.core.domain.AjaxResult;
import com.shensi.common.core.page.TableDataInfo;
import com.shensi.adminCollect.common.exception.BaseException;
import com.shensi.adminCollect.common.utils.StringUtils;
import com.shensi.adminCollect.domain.station.OilOmeter;
import com.shensi.adminCollect.service.station.OilOmeterService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

@RestController
@RequestMapping("/ometer")
@Slf4j
@Api("油罐相关接口")
public class OilOmeterController extends BaseController {

    @Autowired
    private OilOmeterService oilOmeterService;

    @PostMapping("/save")
    @ApiOperation(value = "保存油罐", httpMethod = "POST")
    public AjaxResult saveOilOmeter(@RequestBody OilOmeter oilOmeter){
        if(Objects.isNull(oilOmeter)){
            return AjaxResult.error("保存油罐请求参数不能为空");
        }
        check(oilOmeter);
        log.info("saveOilOmeter 请求参数：[{}]", JSONObject.toJSONString(oilOmeter));
        int i = oilOmeterService.saveOmter(oilOmeter);
        if(i <= 0){
            return AjaxResult.error("保存油罐失败");
        }
        return AjaxResult.success("保存油罐成功");
    }

    @PostMapping("/update")
    @ApiOperation(value = "编辑修改油罐", httpMethod = "POST")
    public AjaxResult updateOilOmeter(@RequestBody OilOmeter oilOmeter){
        if(Objects.isNull(oilOmeter)){
            return AjaxResult.error("保存油罐请求参数不能为空");
        }

        log.info("updateOilOmeter 请求参数：[{}]", JSONObject.toJSONString(oilOmeter));
        int i = oilOmeterService.updateOmter(oilOmeter);
        if(i <= 0){
            return AjaxResult.error("保存油罐失败");
        }
        return AjaxResult.success("保存油罐成功");
    }

    @PostMapping("/list")
    @ApiOperation(value = "查询油罐", httpMethod = "POST")
    public TableDataInfo selectOilOmeterList(@RequestBody OilOmeter oilOmeter){

        startPage();
        log.info("selectOilOmeterList 请求参数：[{}]", JSONObject.toJSONString(oilOmeter));
        List<OilOmeter> oilOmeterList = oilOmeterService.selectOmeterList(oilOmeter);
        log.info("selectOilOmeterList 响应结果：[{}]", oilOmeterList.size());
        return getDataTable(oilOmeterList);
    }

    @GetMapping("/delete")
    @ApiOperation(value = "删除油罐", httpMethod = "GET")
    public AjaxResult deleteOilOmeter(@RequestParam Long ometerId){
        if(Objects.isNull(ometerId)){
            return AjaxResult.error("删除油罐请求参数不能为空");
        }

        log.info("deleteOilOmeter 请求参数：[{}]", ometerId);
        int i = oilOmeterService.deleteOmeter(ometerId);
        if(i <= 0){
            return AjaxResult.error("删除油罐失败");
        }
        return AjaxResult.success("删除油罐成功");
    }

    // 参数校验
    private void check(OilOmeter oilOmeter){
        if(Objects.isNull(oilOmeter.getStationId())){
            throw new BaseException("油站不为空");
        }

        if(StringUtils.isEmpty(oilOmeter.getOilOmeterName())){
            throw new BaseException("油罐名不为空");
        }

        if(StringUtils.isEmpty(oilOmeter.getOmeterGunNum())){
            throw new BaseException("油枪不为空");
        }

        if(Objects.isNull(oilOmeter.getOilId())){
            throw new BaseException("油品不为空");
        }
    }
}
